package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import br.com.bean.NotaFaltaRecuperacaoBean;

public class NotaFaltaRecuperacaoDao {

	public void inserir(Connection conn, NotaFaltaRecuperacaoBean value) throws SQLException {
		String sql = "INSERT INTO nota_falta_recuperacao( nchamada, turma, tipo_recup, " +
				"bimestre, disciplina, nota, falta) VALUES ( ?, ?, ?, ?, ?, ?)";

		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setInt(1, value.getNchamada().getNchamada());
		ps.setInt(2, value.getTurma().getId());
		ps.setInt(2, value.getTiporecup().getId());
		ps.setInt(3, value.getBimestre().getId());
		ps.setInt(4, value.getDisciplina().getId());
		ps.setDouble(5, value.getNota());
		ps.setInt(6, value.getFalta());
		ps.execute();
		ps.close();

	}

	public void excluir(Connection conn, NotaFaltaRecuperacaoBean value) throws Exception {

		String sql = "DELETE FROM nota_falta_recuperacao WHERE id = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.execute();
	}

	public void alterar(Connection conn, NotaFaltaRecuperacaoBean value) throws Exception {

		String sql = "UPDATE nota_falta_recuperacao SET nchamada=?, turma=?, tipo_recup=?, " +
				"bimestre=?, disciplina=?, nota=?, falta=? WHERE  id = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getNchamada().getNchamada());
		ps.setInt(2, value.getTurma().getId());
		ps.setInt(2, value.getTiporecup().getId());
		ps.setInt(3, value.getBimestre().getId());
		ps.setInt(4, value.getDisciplina().getId());
		ps.setDouble(5, value.getNota());
		ps.setInt(6, value.getFalta());
		ps.execute();
		ps.close();
	}

	public ArrayList<NotaFaltaRecuperacaoBean> getLista(Connection conn, NotaFaltaRecuperacaoBean value) throws Exception{

		String sql = " SELECT nchamada, turma, tipo_recup, bimestre, disciplina, " +
				"nota, falta FROM nota_falta_recuperacao where id = ?";

		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<NotaFaltaRecuperacaoBean> al = new ArrayList<NotaFaltaRecuperacaoBean>();
		while(rs.next()){
			NotaFaltaRecuperacaoBean a = new NotaFaltaRecuperacaoBean();
			a.getNchamada().setNchamada(rs.getInt("nchamada"));
			a.getTurma().setId(rs.getInt("turma"));
			a.getTiporecup().setId(rs.getInt("tipo_recup"));
			a.getBimestre().setId(rs.getInt("bimestre"));
			a.getDisciplina().setId(rs.getInt("disciplina"));
			a.setNota(rs.getDouble("nota"));
			a.setFalta(rs.getInt("falta"));
			al.add(a);
		}
		return al;
	}

	public ArrayList<NotaFaltaRecuperacaoBean> getLista(Connection conn) throws Exception{


		String sql = " SELECT nchamada, turma, tipo_recup, bimestre, disciplina, " +
				"nota, falta FROM nota_falta_recuperacao";

		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		ArrayList<NotaFaltaRecuperacaoBean> al = new ArrayList<NotaFaltaRecuperacaoBean>();
		while(rs.next()){
			NotaFaltaRecuperacaoBean a = new NotaFaltaRecuperacaoBean();
			a.getNchamada().setNchamada(rs.getInt("nchamada"));
			a.getTurma().setId(rs.getInt("turma"));
			a.getTiporecup().setId(rs.getInt("tipo_recup"));
			a.getBimestre().setId(rs.getInt("bimestre"));
			a.getDisciplina().setId(rs.getInt("disciplina"));
			a.setNota(rs.getDouble("nota"));
			a.setFalta(rs.getInt("falta"));
			al.add(a);
		}
		return al;
	}

}